<?php
/**
 *大话水浒 技能  、 训练场
 */ 
class Module_overlordskills{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	function index(){
		return $this->barracks();
	}

	/**
	 * 训练场
	 * Enter description here ...
	 */
	function clickgeneral(){
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$smarty = loadSmarty();
		$smarty->assign('id',$id);
		return $smarty->fetch('app/Overlord/clickgeneral.tpl.php');
	} 
	/**
	 * 训练场,进入选将列表
	 * Enter description here ...
	 */
	function clickgeneral_page(){
		$uin = SessionInfo('uin');
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$type = isset($_GET['type']) ? intval($_GET['type']) : null;
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();

		loadLib('App_Overlord_GameStart');	
		loadLib('App_Overlord_Query');	
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		loadLib('App_Overlord_Collocate');
		//获取技能列表
		$data = App_Overlord_Collocate::getSkill();
		// ===============================  检查武将的数据  =====================================
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (!empty($generals_info['Value'])){
			foreach ($generals_info['Value'] as $k => $row){
			//体力恢复
				if (!empty($row['physical_time'])){
				 	if ($row['physical_time'] - $time < 0){
				 		$physical_time = 3600;
						//武将有恢复体力技能
						if (!empty($row['skills']['study'])){
							foreach ($row['skills']['study'] as $j => $skills_row){
								if ($j == 'train_hp'){
									//减短体力恢复时间
									$skills_num = $data['study'][$j][$skills_row]['num'];
									$physical_time -= intval($physical_time * $skills_num/100);
								}
							}
						}
					 	$num = intval(($time - $row['physical_time'])/$physical_time) + 1;
					 	$row['physical'] += $num;
					 	if ($row['physical'] < 10){
					 		$generals_info['Value'][$k]['physical'] += $num;
					 		$end_time = ($time - $row['physical_time'])%$physical_time;
					 		$generals_info['Value'][$k]['physical_time'] = $time + $physical_time - $end_time;
					 	}else{
					 		//满10或超过10点体力,体力都是满
					 		$generals_info['Value'][$k]['physical'] = 10;
					 		//给武将恢复体力时间归0
					 		$generals_info['Value'][$k]['physical_time'] = 0;
					 		//显示武将体力时的赋值
					 	}
					}
				}
				if ($row['state'] != 1){
					$list[$k] = $row;
					$list[$k]['id'] = $k;
					if (!empty($row['train']) && $row['state'] != 3){
						if ($row['train'] - $time > 0){
							$list[$k]['train_time'] = App_Overlord_GameStart::getEndDate($row['train']);
						}else{
							//训练时间
							$generals_info['Value'][$k]['train'] = 0;
							//训练中的状态
							$generals_info['Value'][$k]['state'] = 3;
							//给一个数赋值,有值就修改武将表
							$state = 1;
						}
					}
				}
				//武将升级所需的经验
				$generals_lv = App_Overlord_GameStart::getGeneralsExp($row['lv']);
				if ($row['exp'] >= $generals_lv['upgrade']){
					//武将升级
					$info = App_Overlord_GameStart::getGeneralsPromotion($generals_info['Value'][$k], $generals_lv);
					$generals_info['Value'][$k] = $info;
					$statelv = 1;
				}
			}
			if (!empty($state) || !empty($num) || !empty($statelv)){
				//修改武将训练
				App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
				//跳转训练页面
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordskills&a=clickgeneral_page&type='.$type);
			}
		}
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主资料失败!');
		}
		$smarty = loadSmarty();
		if (!empty($list)){
			$count = count($list);
			/*******************数据分页******************/
			$page = ($page-1) * $per_page;
			$list = array_slice($list,$page,$per_page);
			$smarty->assign('per_page',$per_page);
			$smarty->assign('count',$count);
			$smarty->assign('list',$list);
		}

		$smarty->assign('user_info',$user_info['Value']);
		$smarty->assign('type',$type);
		return $smarty->fetch('app/Overlord/clickgeneral_page.tpl.php');
	} 
	/**
	 * 开始训练
	 * Enter description here ...
	 */
	function clickgeneral_train(){
		$uin = SessionInfo('uin');
		$type = isset($_POST['type']) ? intval($_POST['type']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		if (empty($id)){
			return View::error('参数错误.!');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		// ===============================  检查武将的数据  =====================================
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有武将.');
		}
		if (empty($generals_info['Value'][$id])) {
			return View::error('没有该武将信息.');
		}
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主资料失败!');
		}
		loadLib('App_Overlord_Collocate');
		$data = App_Overlord_Collocate::getTrainExp($generals_info['Value'][$id]['lv']);
		
		if (empty($data) || empty($data[$type])){
			return View::error('无可用数据.');
		}
		//获取任务类型
		loadLib('App_Overlord_Task');
		$task_type = App_Overlord_Task::getTaskType();
		if ($type == 2){
			//完成强化统御任务
			App_Overlord_Task::getGo('main' , $task_type['main']['staff']);
			$name = '统御训练';
		}elseif ($type == 1){
			//完成钻研武技任务
			App_Overlord_Task::getGo('main' , $task_type['main']['energy']);
			$name = '武技训练';
		}elseif ($type == 0){
			//完成日常训练任务
			App_Overlord_Task::getGo('main' , $task_type['main']['everyday']);
			$name = '日常训练';
		}
		$user_info['Value']['gold'] -= 1000;
		if ($user_info['Value']['gold'] < 0){
			return View::error('黄金不足,不能进行'.$name);
		}
	//体力恢复
		if (!empty($generals_info['Value'][$id]['physical_time'])){
			 if ($generals_info['Value'][$id]['physical_time'] - $time < 0){
			 	$physical_time = 3600;
				//武将有恢复体力技能
				if (!empty($generals_info['Value'][$id]['skills']['study'])){
					foreach ($generals_info['Value'][$id]['skills']['study'] as $j => $skills_row){
						if ($j == 'train_hp'){
							//减短体力恢复时间
							$skills_num = $data['study'][$j][$skills_row]['num'];
							$physical_time -= intval($physical_time * $skills_num/100);
						}
					}
				}
			 	$num = intval(($time - $generals_info['Value'][$id]['physical_time'])/$physical_time) + 1;
			 	$generals_info['Value'][$id]['physical'] += $num;
				
			 	if ($generals_info['Value'][$id]['physical'] < 10){
			 		$generals_info['Value'][$id]['physical'] += $num;
			 		$end_time = ($time - $generals_info['Value'][$id]['physical_time'])%$physical_time;
			 		$generals_info['Value'][$id]['physical_time'] = $time + $physical_time - $end_time;
			 	}else{
			 		//满10或超过10点体力,体力都是满
			 		$generals_info['Value'][$id]['physical'] = 10;
			 		//给武将恢复体力时间归0
			 		$generals_info['Value'][$id]['physical_time'] = 0;
			 	}
			 }
		}
		if ($generals_info['Value'][$id]['physical'] < 2){
			return View::error('武将体力不足,无法进行'.$name , 0 , array(
						array('name' => '去购买' , 'url' => '/app.php?m=overlorduser&amp;a=mall&amp;type=diamond&amp;article_type=rest&amp;sid=' . sessionInfo('skey')),
					));
		}
		if ($generals_info['Value'][$id]['state'] == 0) {
			//训练花费的时间,4h
			$train_time = $time + 4 * 3600;
			//武将体力恢复时间
			$physical_time = $time + 3600;
			loadLib('App_Overlord_Collocate');
			//获取技能列表
			$data = App_Overlord_Collocate::getSkill();
			//武将有经验加成技能
			if (!empty($generals_info['Value'][$id]['skills']['study'])){
				foreach ($generals_info['Value'][$id]['skills']['study'] as $k => $skills_row){
					if ($k == 'train_hp'){
						//减短体力恢复时间
						$skills_num = $data['study'][$k][$skills_row]['num'];
						$physical_time -= intval(3600 * $skills_num/100);
					}
					if ($k == 'train'){
						//减短训练时间时间
						$skills_trainnum = $data['study'][$k][$skills_row]['num'];
						$train_time -= intval(4 * 3600 * $skills_trainnum/100);
					}
				}
				
			}
			//训练时间,4h
			$generals_info['Value'][$id]['train'] = $train_time;
			$generals_info['Value'][$id]['train_type'] = $type;
			//训练中的状态
			$generals_info['Value'][$id]['state'] = 2;
			$generals_info['Value'][$id]['physical'] -= 2; 				//修改武将体力值
			//满体力时重新赋值
			if ($generals_info['Value'][$id]['physical'] == 8){
				$generals_info['Value'][$id]['physical_time'] = $physical_time; //体力恢复时间
			}
			//修改君主资料
			$code = App_Overlord_Query::alterUser($uin, $user_info['Value']);//修改武将训练
			$code = App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
			//跳转训练页面
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=generaltrain&id='.$id);
		}else{
			if ($generals_info['Value'][$id]['state'] == 1){
				return View::error('武将'.$generals_info['Value'][$id]['name'].'正在驻防,无法进行训练,请先撤防再进行训练.' , 0 ,array(
				array('name' => '去撤防' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
			}else{
				return View::error('武将'.$generals_info['Value'][$id]['name'].'已在训练中.');
			}
		}
	}
	/**
	 * 取消训练
	 */
	function clickgeneral_cancel(){
		$uin = SessionInfo('uin');
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
	
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		if (empty($id)){
			return View::error('参数错误.!');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		// ===============================  检查武将的数据  =====================================
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有武将.');
		}

		if (empty($generals_info['Value'][$id])) {
			return View::error('没有该武将信息.');
		}
		if ($generals_info['Value'][$id]['state'] == 2) {
			//训练时间
			$generals_info['Value'][$id]['train'] = 0;
			//训练中的状态
			$generals_info['Value'][$id]['state'] = 0;
			//修改武将训练
			App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
			//跳转训练页面
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=clickgeneral_cancel&id='.$id);
		}else{
			return View::error('武将已驻防或未训练或训练已完成.');
		}
	}
	/**
	 * 完成训练
	 */
	function clickgeneral_accomplish(){
		$uin = SessionInfo('uin');
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		if (empty($id)){
			return View::error('参数错误.!');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		// ===============================  检查武将的数据  =====================================
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);
		if (empty($generals_info)){
			return View::error('你还没有武将.');
		}

		if (empty($generals_info['Value'][$id])) {
			return View::error('没有该武将信息.');
		}
		
		// ===============================  检查君主的数据  =====================================
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主信息失败.');
		}
		if ($generals_info['Value'][$id]['state'] == 3) {
			//获取训练类型
			$type = $generals_info['Value'][$id]['train_type'];
			//加载配置数据
			loadLib('App_Overlord_Collocate');
			$data = App_Overlord_Collocate::getTrainExp($generals_info['Value'][$id]['lv']);
			if (empty($data) || empty($data[$type])){
				return View::error('无可用数据.');
			}
			//取得武将可升最高级别
			$generals_max_lv = App_Overlord_GameStart::getGeneralsLevel($user_info['Value']['lv']);
			
			$exp = $data[$type];
			if ($generals_max_lv <= $generals_info['Value'][$id]['lv']){
				$exp = 0;
			}
			//武将加经验
			$generals_info['Value'][$id]['exp'] += $exp;
			//训练时间
			$generals_info['Value'][$id]['train'] = 0;
			//训练中的状态
			$generals_info['Value'][$id]['state'] = 0;
			if ($generals_info['Value'][$id]['train_type'] == 1 || $generals_info['Value'][$id]['train_type'] == 2){
				
				//几率
				$odds = App_Overlord_GameStart::getChance(100);
				
				if (!empty($odds)){
					//技能加载
					$skill = App_Overlord_Collocate::getSkill();
					//统筹
					if ($generals_info['Value'][$id]['train_type'] == 2){
						//随机技能
						$skill_rand = array_rand($skill['strengthen'] , 1);
						$skill_type = 'strengthen';
					}elseif ($generals_info['Value'][$id]['train_type'] == 1){
					//武技
						//随机技能
						$skill_rand = array_rand($skill['study'] , 1);
						$skill_type = 'study';
					}
				
					//详细武将信息
					$generals_row = $generals_info['Value'][$id];
									
					
					//技能初始等级
					$skill_lv = 0;
						//看下该本技能书有几个等级
					$skill_count = count($skill[$skill_type][$skill_rand]) - 1;
					if (!empty($generals_info['Value'][$id]['skills'])){
							
						foreach ($generals_info['Value'][$id]['skills'] as $k => $row){
							foreach ($row as $j => $rs){
								//已学这个技能
								if ($skill_rand == $j){
									if ($skill_count != 0 && $rs == 0){
										//技能等级是二级
										$generals_info['Value'][$id]['skills'][$k][$j] = 1;
										$skill_lv = 1;
									}
									//是否开到已学技能
									$skill_state = 1;
								}
							}
						}
						//如果不是相同的技能,加一个,且还有可学习技能的空格
						if (empty($skill_state) && $generals_info['Value'][$id]['num'] > 0){
							//存一个数组秘籍
							$array = 0;
							$generals_info['Value'][$id]['skills'][$skill_type][$skill_rand] = 0;
							$generals_info['Value'][$id]['num'] -= 1; 
						}	
					}else{
						//没学过技能的武将,包括没有天生技能
						//存一个数组秘籍
						$array = array();
						
						$array[$skill_rand] = 0;
						$skill_arr[$skill_type] = $array;
						$generals_info['Value'][$id]['skills'] = $skill_arr;
						$generals_info['Value'][$id]['num'] -= 1; 
					}
					//查询武将可带兵数
					$is_count = App_Overlord_GameStart::getGeneralsBarracksLv($generals_row['lv']);
									
					//武器攻击初始
					$grow_ap_num = 0;
					if (!empty($generals_row['equipment']['weapon']['num'])){
						$grow_ap_num = $generals_row['equipment']['weapon']['num'];
					}
					//盔甲防御初始
					$grow_defense_num = 0;
					if (!empty($generals_row['equipment']['armour']['num'])){
						$grow_defense_num = $generals_row['equipment']['armour']['num'];
					}
					//头盔
					$grow_hp_num = 0;
					if (!empty($generals_row['equipment']['helm']['num'])){
						$grow_hp_num = $generals_row['equipment']['helm']['num'];
					}
					//初始化武将带兵数
					$make_count = $is_count['count'];
					//初始化攻击
					$grow_ap = $generals_info['Value'][$id]['ap'] - $grow_ap_num;
					//初始化防御力
					$grow_defense = $generals_info['Value'][$id]['defense'] - $grow_defense_num;
					//初始化生命力
					$grow_hp = $generals_info['Value'][$id]['hp'] - $grow_hp_num;
									
					if (!empty($generals_info['Value'][$id]['skills']['strengthen'])){
						//如果有加兵技能
						foreach ($generals_info['Value'][$id]['skills']['strengthen'] as $k => $skills_row){
							if ($k == 'staff' && $k == $skill_rand){
								//带兵加成
								$skills_num = $skill['strengthen'][$k][$skills_row]['num'];
								$make_count += $make_count * $skills_num/100;
							}
						}
					}
					if (!empty($generals_info['Value'][$id]['skills']['study'])){
						//如果有攻击加成技能
						foreach ($generals_info['Value'][$id]['skills']['study'] as $k => $skills_row){
							if ($k == 'ap' && $k == $skill_rand){
								//攻击力加成
								$skills_num = $skill['study'][$k][$skills_row]['num'];
								$grow_ap += $grow_ap * $skills_num/100;
								$grow_ap += $grow_ap_num;
								$generals_info['Value'][$id]['ap'] = $grow_ap;
							}elseif ($k == 'defense' && $k == $skill_rand){
								//防御力加成
								$skills_num = $skill['study'][$k][$skills_row]['num'];
								$grow_defense += $grow_defense * $skills_num/100;
								$grow_defense += $grow_defense_num;
								$generals_info['Value'][$id]['defense'] = $grow_defense;
							}elseif ($k == 'hp' && $k == $skill_rand){
								//生命力加成
								$skills_num = $skill['study'][$k][$skills_row]['num'];
								$grow_hp += $grow_hp * $skills_num/100;
								$grow_hp += $grow_hp_num;
								$generals_info['Value'][$id]['hp'] = $grow_hp;
							}
						}
					}
					//战马
					if (!empty($generals_info['Value'][$id]['equipment']['horse']['num'])){
						$make_count += $generals_row['equipment']['horse']['num'];
					}
					//战马
					$generals_info['Value'][$id]['make_count'] = $make_count;
				}
			}

			//获得君主经验
			$user_info['Value']['exp'] += 2;
			//修改君主信息
			App_Overlord_Query::alterUser($uin, $user_info['Value']);//修改武将训练
			App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
			if (!empty($odds)){
				//跳转训练页面
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=clickgeneral_accomplish&id='.$id.'&odds='.$odds.'&skills_type='.$skill_type.'&skill_rand='.$skill_rand.'&skill_lv='.$skill_lv);
			}else{
				//跳转训练页面
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=clickgeneral_accomplish&id='.$id);
			}
		}else{
			return View::error('武将已驻防或未训练或训练未完成.');
		}
	}
	/**
	 * 武将技能
	 * Enter description here ...
	 */
	function generalsskills(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$to_uin = isset($_GET['to_uin']) ? intval($_GET['to_uin']) : 0; //外界传递的to_uin
		
		if (empty($to_uin)){
			$uin = sessionInfo('uin');
			if (empty($uin)){
				return View::error('获取君主ID失败.');
			}
		}else{
			$uin = $to_uin;
		}
		
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$result = App_Overlord_Query::oneUin_Generals($uin);

		if (!empty($result['Value'][$id])){
			//技能列表
			loadLib('App_Overlord_Collocate');   
			$data = App_Overlord_Collocate::getSkill();
			//武将信息
			$generals_info = $result['Value'][$id];
		
			$smarty->assign('generals_info',$generals_info);
			$smarty->assign('data',$data);
			$smarty->assign('id',$id);
			$smarty->assign('to_uin',$to_uin);
		return $smarty->fetch('app/Overlord/generalsskills.tpl.php');
		}else{
			return View::error('数据异常.');
		}
	}
}

# end of script